//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "TimeGrid.h"
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CTimeGrid::CTimeGrid (Double end, UInt64 steps) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phTimeGrid = NULL;
#endif
        QuantLib::Time _end = (QuantLib::Time)ValueHelper::Convert (end);
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps);
        _ppTimeGrid = new boost::shared_ptr<QuantLib::TimeGrid> (new QuantLib::TimeGrid ( _end,  _steps ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CTimeGrid::CTimeGrid () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phTimeGrid = NULL;
#endif
        _ppTimeGrid = new boost::shared_ptr<QuantLib::TimeGrid> (new QuantLib::TimeGrid ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CTimeGrid::CTimeGrid (boost::shared_ptr<QuantLib::TimeGrid>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeGrid = NULL;
#endif
	_ppTimeGrid = &childNative;
    
}
Cephei::QL::CTimeGrid::CTimeGrid (QuantLib::TimeGrid& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTimeGrid = NULL;
#endif
	_ppTimeGrid = new boost::shared_ptr<QuantLib::TimeGrid> (&childNative);
    
    _TimeGridOwner = owner;
    
}

Cephei::QL::CTimeGrid::CTimeGrid (CTimeGrid^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeGrid = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTimeGrid = new boost::shared_ptr<QuantLib::TimeGrid> (copy->GetShared());
        
    }
}
Cephei::QL::CTimeGrid::CTimeGrid (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeGrid = NULL;
#endif
	if (!t->IsSubclassOf(CTimeGrid::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CTimeGrid::CTimeGrid (QuantLib::Handle<QuantLib::TimeGrid>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTimeGrid = &childNative;
	_ppTimeGrid = &static_cast<boost::shared_ptr<QuantLib::TimeGrid>>(childNative.currentLink());
    
    _TimeGridOwner = owner;
}
Cephei::QL::CTimeGrid::CTimeGrid (QuantLib::Handle<QuantLib::TimeGrid> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTimeGrid = &childNative;
	_ppTimeGrid = &static_cast<boost::shared_ptr<QuantLib::TimeGrid>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CTimeGrid::CTimeGrid (QuantLib::TimeGrid childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTimeGrid = NULL;
#endif
	_ppTimeGrid = new boost::shared_ptr<QuantLib::TimeGrid> (new QuantLib::TimeGrid (childNative));
    
}
#endif

Cephei::QL::CTimeGrid::~CTimeGrid ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTimeGrid != NULL)
    {
	    delete _ppTimeGrid;
        _ppTimeGrid = NULL;
    }
}
Cephei::QL::CTimeGrid::!CTimeGrid ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTimeGrid != NULL)
    {
	    delete _ppTimeGrid;
    }
}
QuantLib::TimeGrid& Cephei::QL::CTimeGrid::GetReference ()
{
    if (_ppTimeGrid == NULL) throw gcnew NativeNullException ();
	return **_ppTimeGrid;
}
boost::shared_ptr<QuantLib::TimeGrid>& Cephei::QL::CTimeGrid::GetShared ()
{
    if (_ppTimeGrid == NULL) throw gcnew NativeNullException ();
	return *_ppTimeGrid;
}
QuantLib::TimeGrid* Cephei::QL::CTimeGrid::GetPointer ()
{
    if (_ppTimeGrid == NULL) throw gcnew NativeNullException ();
	return &**_ppTimeGrid;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TimeGrid>& Cephei::QL::CTimeGrid::GetHandle ()
{
	if (_phTimeGrid == NULL)
	{
		_phTimeGrid = new Handle<QuantLib::TimeGrid> (*_ppTimeGrid);
	}
	return *_phTimeGrid;
}
#endif
bool Cephei::QL::CTimeGrid::HasNative () 
{
	return (_ppTimeGrid != NULL);
}

Double Cephei::QL::CTimeGrid::At (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppTimeGrid)->at ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CTimeGrid::Back::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppTimeGrid)->back ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::CTimeGrid::ClosestIndex (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTimeGrid)->closestIndex ( _t );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CTimeGrid::ClosestTime (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppTimeGrid)->closestTime ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CTimeGrid::Dt (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppTimeGrid)->dt ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::CTimeGrid::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppTimeGrid)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CTimeGrid::Front::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppTimeGrid)->front ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::CTimeGrid::Index (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTimeGrid)->index ( _t );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::CTimeGrid::MandatoryTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppTimeGrid)->mandatoryTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::CTimeGrid::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTimeGrid)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::ITimeGrid^ Cephei::QL::CTimeGrid_Factory::Create (Double end, UInt64 steps)
{
    return gcnew CTimeGrid ( end,  steps);
}
Cephei::QL::ITimeGrid^ Cephei::QL::CTimeGrid_Factory::Create ()
{
    return gcnew CTimeGrid ();
}
